/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * RelatorioDePedidosDialog.java
 *
 * Created on Nov 12, 2010, 1:55:20 PM
 */
package br.com.tbp.gui;

import br.com.tbp.controle.ControleConsultor;
import br.com.tbp.controle.ControleRelatorio;
import br.com.tbp.exception.PersistenciaException;
import br.com.tbp.model.domain.Consultor;
import java.awt.Cursor;
import java.util.Date;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

/**
 *
 * @author Diego Freire
 */
public class RelatorioVisitaDialog extends javax.swing.JDialog {

    private Consultor consultor = new Consultor(0, "Todos os consultores");

    public RelatorioVisitaDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo(null);
        data1TextField.setDate(new Date());
        carregarConsultores();
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        separador1 = new javax.swing.JSeparator();
        data1 = new javax.swing.JLabel();
        data1TextField = new org.jdesktop.swingx.JXDatePicker();
        data2 = new javax.swing.JLabel();
        data2TextField = new org.jdesktop.swingx.JXDatePicker();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        consultorCombo = new javax.swing.JComboBox();
        separador3 = new javax.swing.JSeparator();
        jPanel2 = new javax.swing.JPanel();
        limparJButton = new javax.swing.JButton();
        emitirButton = new javax.swing.JButton();
        fecharButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("TBP - Relatório de Visita");

        jPanel1.setPreferredSize(new java.awt.Dimension(475, 190));

        separador1.setForeground(new java.awt.Color(153, 153, 153));

        data1.setText("Data Inicial:");

        data2.setText("Data Final:");

        jLabel1.setForeground(new java.awt.Color(50, 72, 121));
        jLabel1.setText("Selecione a data inicial, a data final, o consultor e clique para emitir as visitas.");

        jLabel2.setText("Consultor:");

        separador3.setForeground(new java.awt.Color(153, 153, 153));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(data2)
                            .addComponent(data1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(data1TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(data2TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(consultorCombo, 0, 332, Short.MAX_VALUE)))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(separador1, javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
            .addComponent(separador3, javax.swing.GroupLayout.DEFAULT_SIZE, 474, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(separador1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(data1TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(data1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(data2)
                    .addComponent(data2TextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(consultorCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addComponent(separador3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        limparJButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/tbp/resources/blank.png"))); // NOI18N
        limparJButton.setMnemonic('l');
        limparJButton.setText("Limpar");
        limparJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                limparJButtonActionPerformed(evt);
            }
        });
        jPanel2.add(limparJButton);

        emitirButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/tbp/resources/magnifier.png"))); // NOI18N
        emitirButton.setMnemonic('e');
        emitirButton.setText("Emitir Relatório");
        emitirButton.setPreferredSize(new java.awt.Dimension(150, 26));
        emitirButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                emitirButtonActionPerformed(evt);
            }
        });
        jPanel2.add(emitirButton);

        fecharButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/tbp/resources/delete.png"))); // NOI18N
        fecharButton.setMnemonic('f');
        fecharButton.setText("Fechar");
        fecharButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fecharButtonActionPerformed(evt);
            }
        });
        jPanel2.add(fecharButton);

        getContentPane().add(jPanel2, java.awt.BorderLayout.PAGE_END);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void limparJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_limparJButtonActionPerformed
        limparCampos();
}//GEN-LAST:event_limparJButtonActionPerformed

    private void emitirButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_emitirButtonActionPerformed
        Date inicio = data1TextField.getDate();
        if (inicio == null) {
            JOptionPane.showMessageDialog(null, "A data inicial deve ser informada.", "Mensagem do sistema", 1);
            return;
        }
        Date fim = data2TextField.getDate();
        if (fim == null) {
            fim = inicio;
        }
        try {
            setCursor(new Cursor(Cursor.WAIT_CURSOR));
            ControleRelatorio.reportVisita(inicio, fim, (Consultor) consultorCombo.getSelectedItem());
        } catch (PersistenciaException pex) {
            System.out.println(pex.getMessage());
        } finally {
            setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        }
}//GEN-LAST:event_emitirButtonActionPerformed

    private void fecharButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fecharButtonActionPerformed
        dispose();
}//GEN-LAST:event_fecharButtonActionPerformed
    // <editor-fold defaultstate="collapsed" desc="Declaration of class members">
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox consultorCombo;
    private javax.swing.JLabel data1;
    private org.jdesktop.swingx.JXDatePicker data1TextField;
    private javax.swing.JLabel data2;
    private org.jdesktop.swingx.JXDatePicker data2TextField;
    private javax.swing.JButton emitirButton;
    private javax.swing.JButton fecharButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JButton limparJButton;
    private javax.swing.JSeparator separador1;
    private javax.swing.JSeparator separador3;
    // End of variables declaration//GEN-END:variables
    // </editor-fold>

    private void limparCampos() {
        data1TextField.setDate(null);
        data2TextField.setDate(null);
        data1TextField.requestFocus();
    }

    private void carregarConsultores() {
        try {
            ControleConsultor controle = new ControleConsultor();
            DefaultComboBoxModel model = new DefaultComboBoxModel(controle.pesquisar2().toArray());
            model.addElement(consultor);
            consultorCombo.setModel(model);
            consultorCombo.setSelectedItem(consultor);
        } catch(PersistenciaException pex) {
            System.out.println(pex.getMessage());
        }
    }

    public static void main(String[] args) {
        new RelatorioVisitaDialog(null, false).setVisible(true);
    }
}
